import Vue from 'vue'
import VueRouter from 'vue-router'

// 添加这个Login组件
import Login from '../components/Login.vue'
import Welcome from '../components/common/Welcome.vue'
import Home from '../components/Home.vue'
import UpdatePassword from '../components/common/UpdatePassword.vue'

// user下面的两个功能
import MostHotCard from '../components/user/MostHotCard.vue'
import MostNewCard from '../components/user/MostNewCard.vue'
import WriteCard from '../components/user/WriteCard.vue'

//admin下面的两个功能
import AuthorizationManage from '../components/admin/AuthorizationManage.vue'
import UserManage from '../components/admin/UserManage.vue'

//系统咋像下面的两个内容
import LogInfo from '../components/common/LogInfo.vue'





Vue.use(VueRouter)

const routes = [{
    path: '/',
    component: Login
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/updatePassword',
        component: UpdatePassword
      },
      {
        path: '/mostHotCard',
        component: MostHotCard
      },
      {
        path: '/mostNewCard',
        component: MostNewCard
      },
      {
        path: '/writeCard',
        component: WriteCard
      },
      {
        path: '/authorizationManage',
        component: AuthorizationManage
      },
      {
        path: '/userManage',
        component: UserManage
      },
      {
        path: '/logInfo',
        component: LogInfo
      },

    ]
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  //to 将要访问
  //from 从哪进行访问
  //next 重定向这个url next() 继续访问。to的路径
  if (to.path == '/login') return next(); //如果是访问这个login，那么就放行，然后执行之后的操作，也就是说这个是首页
  const userFlag = window.sessionStorage.getItem("user"); //如果不是首页，那么就判断这个信息是不是有这个sessionStorage里面进行判断，
  if (!userFlag) { //没有这个信息
    return next('/login') //转到登录的地方去，无值返回到登陆页面    //如果没有，那么就到登录的页面
  }
  next(); //有值，那么就直接进行放行
})





export default router